/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.anny.deliveryaddress.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.anny.deliveryaddress.entity.AnnyDeliveryAddress;
import com.jeeplus.modules.anny.deliveryaddress.service.AnnyDeliveryAddressService;

/**
 * 收获地址管理Controller
 * @author clf
 * @version 2018-10-18
 */
@Controller
@RequestMapping(value = "${adminPath}/deliveryaddress/annyDeliveryAddress")
public class AnnyDeliveryAddressController extends BaseController {

	@Autowired
	private AnnyDeliveryAddressService annyDeliveryAddressService;
	
	@ModelAttribute
	public AnnyDeliveryAddress get(@RequestParam(required=false) String id) {
		AnnyDeliveryAddress entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = annyDeliveryAddressService.get(id);
		}
		if (entity == null){
			entity = new AnnyDeliveryAddress();
		}
		return entity;
	}
	
	/**
	 * 收货地址信息列表页面
	 */
	@RequiresPermissions("deliveryaddress:annyDeliveryAddress:list")
	@RequestMapping(value = {"list", ""})
	public String list() {
		return "modules/anny/deliveryaddress/annyDeliveryAddressList";
	}
	
		/**
	 * 收货地址信息列表数据
	 */
	@ResponseBody
	@RequiresPermissions("deliveryaddress:annyDeliveryAddress:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(AnnyDeliveryAddress annyDeliveryAddress, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AnnyDeliveryAddress> page = annyDeliveryAddressService.findPage(new Page<AnnyDeliveryAddress>(request, response), annyDeliveryAddress); 
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加，编辑收货地址信息表单页面
	 */
	@RequiresPermissions(value={"deliveryaddress:annyDeliveryAddress:view","deliveryaddress:annyDeliveryAddress:add","deliveryaddress:annyDeliveryAddress:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(AnnyDeliveryAddress annyDeliveryAddress, Model model) {
		model.addAttribute("annyDeliveryAddress", annyDeliveryAddress);
		if(StringUtils.isBlank(annyDeliveryAddress.getId())){//如果ID是空为添加
			model.addAttribute("isAdd", true);
		}
		return "modules/anny/deliveryaddress/annyDeliveryAddressForm";
	}

	/**
	 * 保存收货地址信息
	 */
	@RequiresPermissions(value={"deliveryaddress:annyDeliveryAddress:add","deliveryaddress:annyDeliveryAddress:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(AnnyDeliveryAddress annyDeliveryAddress, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, annyDeliveryAddress)){
			return form(annyDeliveryAddress, model);
		}
		//新增或编辑表单保存
		annyDeliveryAddressService.save(annyDeliveryAddress);//保存
		addMessage(redirectAttributes, "保存收货地址信息成功");
		return "redirect:"+Global.getAdminPath()+"/deliveryaddress/annyDeliveryAddress/?repage";
	}
	
	/**
	 * 删除收货地址信息
	 */
	@ResponseBody
	@RequiresPermissions("deliveryaddress:annyDeliveryAddress:del")
	@RequestMapping(value = "delete")
	public AjaxJson delete(AnnyDeliveryAddress annyDeliveryAddress, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		annyDeliveryAddressService.delete(annyDeliveryAddress);
		j.setMsg("删除收货地址信息成功");
		return j;
	}
	
	/**
	 * 批量删除收货地址信息
	 */
	@ResponseBody
	@RequiresPermissions("deliveryaddress:annyDeliveryAddress:del")
	@RequestMapping(value = "deleteAll")
	public AjaxJson deleteAll(String ids, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		String idArray[] =ids.split(",");
		for(String id : idArray){
			annyDeliveryAddressService.delete(annyDeliveryAddressService.get(id));
		}
		j.setMsg("删除收货地址信息成功");
		return j;
	}
	
//	/**
//	 * 导出excel文件
//	 */
//	@ResponseBody
//	@RequiresPermissions("deliveryaddress:annyDeliveryAddress:export")
//    @RequestMapping(value = "export", method=RequestMethod.POST)
//    public AjaxJson exportFile(AnnyDeliveryAddress annyDeliveryAddress, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
//		AjaxJson j = new AjaxJson();
//		try {
//            String fileName = "收货地址信息"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
//            Page<AnnyDeliveryAddress> page = annyDeliveryAddressService.findPage(new Page<AnnyDeliveryAddress>(request, response, -1), annyDeliveryAddress);
//    		new ExportExcel("收货地址信息", AnnyDeliveryAddress.class).setDataList(page.getList()).write(response, fileName).dispose();
//    		j.setSuccess(true);
//    		j.setMsg("导出成功！");
//    		return j;
//		} catch (Exception e) {
//			j.setSuccess(false);
//			j.setMsg("导出收货地址信息记录失败！失败信息："+e.getMessage());
//		}
//			return j;
//    }
//
//	/**
//	 * 导入Excel数据
//
//	 */
//	@RequiresPermissions("deliveryaddress:annyDeliveryAddress:import")
//    @RequestMapping(value = "import", method=RequestMethod.POST)
//    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
//		try {
//			int successNum = 0;
//			int failureNum = 0;
//			StringBuilder failureMsg = new StringBuilder();
//			ImportExcel ei = new ImportExcel(file, 1, 0);
//			List<AnnyDeliveryAddress> list = ei.getDataList(AnnyDeliveryAddress.class);
//			for (AnnyDeliveryAddress annyDeliveryAddress : list){
//				try{
//					annyDeliveryAddressService.save(annyDeliveryAddress);
//					successNum++;
//				}catch(ConstraintViolationException ex){
//					failureNum++;
//				}catch (Exception ex) {
//					failureNum++;
//				}
//			}
//			if (failureNum>0){
//				failureMsg.insert(0, "，失败 "+failureNum+" 条收货地址信息记录。");
//			}
//			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条收货地址信息记录"+failureMsg);
//		} catch (Exception e) {
//			addMessage(redirectAttributes, "导入收货地址信息失败！失败信息："+e.getMessage());
//		}
//		return "redirect:"+Global.getAdminPath()+"/deliveryaddress/annyDeliveryAddress/?repage";
//    }
//
//	/**
//	 * 下载导入收货地址信息数据模板
//	 */
//	@RequiresPermissions("deliveryaddress:annyDeliveryAddress:import")
//    @RequestMapping(value = "import/template")
//    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
//		try {
//            String fileName = "收货地址信息数据导入模板.xlsx";
//    		List<AnnyDeliveryAddress> list = Lists.newArrayList();
//    		new ExportExcel("收货地址信息数据", AnnyDeliveryAddress.class, 1).setDataList(list).write(response, fileName).dispose();
//    		return null;
//		} catch (Exception e) {
//			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
//		}
//		return "redirect:"+Global.getAdminPath()+"/deliveryaddress/annyDeliveryAddress/?repage";
//    }

}